package com.dartfar.base.action;

import java.util.List;

import org.dom4j.Element;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
import org.jbpm.api.task.Task;
import org.jbpm.pvm.internal.repository.DeploymentImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dartfar.workflow.helper.WorkflowHelper;


public class Test {
	
	public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        applicationContext.start();
        ProcessEngine processEngine = (ProcessEngine)applicationContext.getBean("processEngine");
        ExecutionService executionService = processEngine.getExecutionService();
        TaskService taskService = processEngine.getTaskService();
        //发布流程
        String deploymentId = processEngine.getRepositoryService().createDeployment()
        .addResourceFromClasspath("swing.jpdl.xml").deploy();
        
        WorkflowHelper wfh = new WorkflowHelper("E:\\Workspace\\ClientSafeManage\\src\\swing.jpdl.xml");
		//拿到流程定义节点
		Element process=wfh.getProcessNode();
        
		String name = process.attributeValue("name");
		String key = process.attributeValue("key");
		
		System.out.println("name: " + name);
		System.out.println("key: " + key);
		
        System.out.println("流程发布ID:"+deploymentId);
        RepositoryService repositoryService = processEngine.getRepositoryService();
		//2.得到已部署的流程定义对象
		DeploymentImpl deployementImpl=(DeploymentImpl) repositoryService.createDeploymentQuery().deploymentId(deploymentId).uniqueResult();
		System.out.println("dddddd: " + deployementImpl.getProcessDefinitionVersion(process.attributeValue("name")));
        
        //启动一个流程实例
        ProcessInstance processInstance = executionService.startProcessInstanceByKey("swing");
        System.out.println("流程实例ID:" + processInstance.getId());
        //A处理任务
        List<Task> taskList_A = taskService.findPersonalTasks("A");
        System.out.println("A待处理任务数:" + taskList_A.size());
        if(taskList_A.size() > 0){
            Task task = taskList_A.get(0);
            taskService.completeTask(task.getId());
        }
        
        //B处理任务
        List<Task> taskList_B = taskService.findPersonalTasks("B");
        System.out.println("B待处理任务数:" + taskList_B.size());
        if(taskList_B.size() > 0){
            Task task = taskList_B.get(0);
            taskService.completeTask(task.getId());
        }

}



}
